package com.turtledove.storyalbum.control;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.turtledove.storyalbum.R;
import com.turtledove.storyalbum.model.helper.DBHandler;

public class WidgetSettingsActivity extends Activity implements OnClickListener {

	LinearLayout lnChangeAlbum, lnSlideShowInterval, lnAbout, lnHelp;
	TextView txtSlideShowInterval;
	Dialog dialog;
	int intervalCurent;
	Context mContext;
	ImageView ic_back;

	// _______________________ System Fuction ________________________________

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_widget_setting);
		mContext = this;
		DBHandler.initdb(mContext);
		initWidget();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.lnChangeAlbum:
			Intent intent = new Intent(this, AlbumManageActivity.class);
			startActivity(intent);
			break;
		case R.id.lnSlideShowInterval:
			showDialogInterval();
			break;
		case R.id.ic_back:
			finish();
			break;
		case R.id.lnAbout:
			Intent intentAbout = new Intent(mContext, AboutActivity.class);
			startActivity(intentAbout);
			break;
		case R.id.lnHelp:
			Intent intentHelp = new Intent(mContext, HelpActivity.class);
			startActivity(intentHelp);
			break;
		default:
			break;
		}

	}

	// _______________________________- Custom Function ________________________

	private void initWidget() {
		try {
			ic_back = (ImageView)findViewById(R.id.ic_back);
			ic_back.setOnClickListener(this);
			
			lnHelp = (LinearLayout)findViewById(R.id.lnHelp);
			lnHelp.setOnClickListener(this);
			
			lnAbout = (LinearLayout)findViewById(R.id.lnAbout);
			lnAbout.setOnClickListener(this);
			
			lnChangeAlbum = (LinearLayout) findViewById(R.id.lnChangeAlbum);
			lnChangeAlbum.setOnClickListener(this);

			lnSlideShowInterval = (LinearLayout) findViewById(R.id.lnSlideShowInterval);
			lnSlideShowInterval.setOnClickListener(this);
			txtSlideShowInterval = (TextView) findViewById(R.id.txtSlideShowInterval);
			setTextInterval(String.valueOf(DBHandler.getInterval(mContext)));
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	private void setTextInterval(String strInterval) {
		txtSlideShowInterval.setText(strInterval + " " + getResources().getString(R.string.seconds));
	}

	private void showDialogInterval() {
		try {
			dialog = new Dialog(this);
			dialog.setTitle(getResources().getString(R.string.slideshow_interval));
			dialog.setContentView(R.layout.dialog_interval);
			final EditText dialogInterval_edTime = (EditText) dialog
					.findViewById(R.id.dialogInterval_edTime);
			Button dialog_BtnYes = (Button) dialog
					.findViewById(R.id.dialog_BtnYes);
			Button dialog_BtnNo = (Button) dialog
					.findViewById(R.id.dialog_BtnNo);
			dialog_BtnYes.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					int interval = 3;
					try {
						interval = Integer.parseInt(dialogInterval_edTime.getText().toString());
						if(interval < 3)
						{
							interval = 3;
							Toast.makeText(mContext, mContext.getResources().getString(R.string.interval_must_be_gather_than_3), Toast.LENGTH_LONG).show();
						}
					} catch (Exception e) {
						// TODO: handle exception
					}
					setTextInterval(String.valueOf(interval));
					DBHandler.updateInterval(mContext, String.valueOf(interval));
					WidgetUpdateService.updateInterval = true;
					dialog.dismiss();
				}
			});
			dialog_BtnNo.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					dialog.dismiss();
				}
			});
			dialog.show();
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
}
